package com.neuedu.mblog.controller.admin;

import com.neuedu.mblog.controller.BaseController;
import com.neuedu.mblog.entiry.Role;
import com.neuedu.mblog.entiry.ShiroRole;
import com.neuedu.mblog.service.RoleService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

/**
 * 项目：mblog_java4
 * 创建时间：  2021-12-29   9:19
 * 作者 :jshand
 * 描述 : 角色的后台管理系统
 * /admin/chanel
 *
 * http://localhost/mblog/admin/role
 * http://localhost/mblog/admin/role?_type=list
 */
@WebServlet(urlPatterns = "/admin/role")
public class AdminRoleController extends BaseController {
    private RoleService channelService = new RoleService();
    @Override
    protected void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        List<ShiroRole> list = channelService.selectList(request);
        request.setAttribute("list",list);
        request.getRequestDispatcher("/WEB-INF/view/admin/role/role_list.jsp").forward(request,response);

    }

    @Override
    protected void ajaxDelete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        boolean success = channelService.deleteById(request);

        //返回json数据
       super.jsonResult(response,success);
    }

    @Override
    protected void toAdd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher("/WEB-INF/view/admin/role/role_add.jsp").forward(request,response);
    }

    @Override
    protected void save(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        boolean success = channelService.save(request);

        if(success){
            response.sendRedirect(request.getContextPath()+"/admin/role");
        }else{
            request.getRequestDispatcher("/error.jsp").forward(request,response);
        }
    }

    @Override
    protected void toEdit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {

        ShiroRole role = channelService.selectById(request);
        request.setAttribute("role",role);
        request.getRequestDispatcher("/WEB-INF/view/admin/role/role_edit.jsp").forward(request,response);

    }

    @Override
    protected void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {

       boolean success = channelService.update(request);

       if(success){
            response.sendRedirect(request.getContextPath()+"/admin/role");
       }else{
           request.getRequestDispatcher("/error.jsp").forward(request,response);
       }

    }
}
